<template>
  <div style="position: relative">
    <!-- <Bg /> -->
    <el-container class="wrap">
      <el-aside :width="asideWidth" class="h100 aside">
        <basic-container class="h100">
          <el-radio-group :size="size" v-model="$store.state.common.team" class="teamGroup"
            @change="changeTeam($store.state.common.team)">
            <el-radio-button :label="item.id" v-for="item in teamList" :key="item.id">{{
        item.name
      }}</el-radio-button>
          </el-radio-group>
        </basic-container>
      </el-aside>
      <el-main class="h100 flex flex-col">
        <div>
          <basic-container :class="asideWidth == '0px' ? 'hide' : ''">
            <el-radio-group v-model="$store.state.common.sequence" :size="size">
              <el-radio-button :label="item.id" v-for="item in sequenceList" :key="item.id">
                {{ item.name }}
              </el-radio-button>
            </el-radio-group>
          </basic-container>
          <basic-container class="right" :class="asideWidth == '0px' ? 'pt0' : ''" v-if="mapData.length">
            <div class="box">
              <div class="line"></div>
              <div v-for="item in mapData" :key="item.id" class="mr50 positionSequence">
                <div>
                  <EBtn :text="item.name" type="11" :color="setColor(item.status)" />
                </div>
                <div v-if="item.children && item.children.length">
                  <div class="mt15 position content" v-for="pItem in item.children" :key="pItem.id">
                    <EBtn :text="pItem.name" type="10" :color="setColor(pItem.status)"
                      @click="openStudyDialog(pItem, item)" />
                    <div class="mt15 position direction" v-for="dItem in pItem.children" :key="dItem.id">
                      <EBtn :text="dItem.name" type="10" :color="setColor(dItem.status)"
                        @click="openStudyDialog(pItem, item, dItem)" />
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </basic-container>
          <basic-container :class="asideWidth == '0px' ? 'pt0' : ''" v-else>
            <h2 class="text-center">欢迎登陆员工岗位能力与培训管理平台</h2>
          </basic-container>
        </div>
      </el-main>
      <el-dialog v-model="isShow" :title="title" @close="isShow = false">
        <el-descriptions border :column="2" center>
          <el-descriptions-item :label="item.classificationName" v-for="item in classification"
            :key="item.classificationId">{{ item.studyTimeHour }}小时 / {{ item.requireTimeHour
            }}小时</el-descriptions-item>
        </el-descriptions>
        <el-descriptions border :column="2" class="center">
          <el-descriptions-item label="本岗位累计学时" :span="1">{{ sumStudyTimeHour }}小时 / {{ sumRequireTimeHour
            }}小时</el-descriptions-item>
        </el-descriptions>
        <template #footer>
          <div class="text-center">
            <el-button type="primary" @click="toStudy">在线学习</el-button>
            <el-button type="primary" :disabled="!canExam" @click="toExam" v-if="state == 2">
              在线考核
            </el-button>
            <el-button type="primary" @click="toExam" v-else-if="state == 3"> 继续考核 </el-button>
            <el-button type="primary" :disabled="true" v-else> 考核结束 </el-button>
            <el-button type="primary" v-if="isDev" @click="toExam">
              在线考核TEST
            </el-button>
          </div>
        </template>
      </el-dialog>
    </el-container>
    <el-dialog v-model="showExam" @close="showExam = false" :fullscreen="true">
      <Exam :title="title" :id="currentData.id" :examRule="examRule" :state="state" @close="endExam" />
    </el-dialog>
  </div>
</template>

<script>
import { getTeamList, getSequenceList, getPositionSequenceList, getMap } from '@/api/position/team';
import { getList } from '@/api/position/positionInstance';
import { getDirListByInstance } from '@/api/position/direction';
import { mapGetters } from 'vuex';
import { getDirDetail } from '@/api/position/direction';
import { getDetail, getStudyTime } from '@/api/position/positionInstance';
import Bg from './bg.vue';
import EBtn from './button.vue';
import { examInstance } from '@/api/position/positionInstance';
import Exam from '@/views/exam/index.vue';

export default {
  components: { Bg, EBtn, Exam },
  name: 'wel',
  data() {
    return {
      positionSequence: '',
      positionSequenceData: {},
      positionData: {},
      directionData: {},
      isShow: false,
      title: '',
      currentData: {},
      classification: [],
      sumRequireTimeHour: '',
      sumStudyTimeHour: '',
      activeIndexMap: {
        psIndex: 1,
        pIndex: 0,
        dIndex: 0,
      },
      canExam: false,
      mapData: [],
      state: 2,
      examRule: [],
      showExam: false,
    };
  },
  computed: {
    ...mapGetters(['team', 'teamList', 'sequence', 'sequenceList', 'isOpen', 'size']),
    asideWidth() {
      return this.isOpen ? '200px' : '0px';
    },
    isDev(){
      return import.meta.env.DEV;
    }
  },
  watch: {
    async sequence(e) {
      this.positionSequenceData = [];
      this.positionData = {};
      this.directionData = {};
      this.mapData = [];
      if (e) {
        const map = await getMap(e);
        this.mapData = map.data.data;
      }
    },
    // team(id){
    //   this.getSequenceList(id);
    // }
  },
  mounted() {
    const teamId = sessionStorage.getItem('teamId');
    if (teamId) {
      this.getSequenceList(teamId);
    }
  },
  methods: {
    changeTeam(id) {
      this.getSequenceList(id);
    },
    // 获取序列列表
    async getSequenceList(id) {
      this.$store.commit('SET_TEAM', id);
      const res = await getSequenceList(id);
      if (res.data.data.length) {
        let sequenceId = sessionStorage.getItem('sequenceId');
        if (!sequenceId) {
          sequenceId = res.data.data[0].id;
        } else {
          const sequence = res.data.data.find(item => item.id == sequenceId);
          if (!sequence) {
            sequenceId = res.data.data[0].id;
          }
        }
        this.$store.commit('SET_SEQUENCE', sequenceId);
        this.$store.commit('SET_SEQUENCELIST', res.data.data);
        sessionStorage.setItem('sequenceId', sequenceId);
        const map = await getMap(sequenceId);
        this.mapData = map.data.data;
      } else {
        this.$store.commit('SET_SEQUENCE', '');
        this.$store.commit('SET_SEQUENCELIST', []);
        sessionStorage.setItem('sequenceId', '');
      }
      // if (sequenceId) {
      //   this.$store.commit('SET_SEQUENCE', sequenceId);
      //   const sequenceData = res.data.data.find(item => item.id == sequenceId);
      //   if (sequenceData) {
      //     this.$store.commit('SET_SEQUENCE', sequenceId);
      //     this.getPositionSequenceList(sequenceId);
      //   }
      // }
    },
    // 获取岗位序列列表
    // async getPositionSequenceList(id) {
    //   this.$store.commit('SET_SEQUENCE', id);
    //   this.positionSequenceData = [];
    //   const res = await getPositionSequenceList(id);
    //   this.positionSequenceData = res.data.data;
    //   if (res.data.data.length) {
    //     res.data.data.forEach(item => {
    //       this.getPositionList(item.id);
    //     });
    //   }
    // },
    // // 获取岗位
    // async getPositionList(id) {
    //   const res = await getList(id);
    //   this.positionData[id] = res.data.data.records;
    //   if (res.data.data.records) {
    //     res.data.data.records.forEach((item, index) => {
    //       this.getDirectionList(item.id, item.position.id);
    //     });
    //   }
    // },
    // // 获取方向列表
    // async getDirectionList(id, pId) {
    //   const res = await getDirListByInstance(id);
    //   this.directionData[pId] = res.data.data.records;
    // },
    // 设置岗位序列选中状态
    setColor(status) {
      switch (status) {
        case 1:
          return 'warning';
        case 2:
          return 'primary';
        case 0:
          return 'danger';
      }
    },
    async openStudyDialog(data, item, dItem) {
      if (!dItem && data.children.length) {
        return;
      }
      this.classification = [];
      this.sumStudyTimeHour = '';
      this.sumRequireTimeHour = '';
      this.currentData = dItem || data;
      this.isShow = true;
      this.title =
        (dItem && dItem.product ? dItem.product : '') +
        (data && data.product ? data.product : '') +
        data.name +
        (dItem ? `(${dItem.name})` : '');
      const studyTime = await getStudyTime(dItem ? dItem.id : data.id);
      this.classification = studyTime.data.data.classification;
      this.sumStudyTimeHour = studyTime.data.data.sumStudyTimeHour;
      this.sumRequireTimeHour = studyTime.data.data.sumRequireTimeHour;
      this.canExam = studyTime.data.data.canExam;
      const exam = await examInstance(this.currentData.id);
      const { state, message, examRule } = exam.data.data;
      this.state = state;
      this.examRule = examRule;
      if (state != 2) {
        this.$message.error(message);
        return;
      }
    },
    toStudy() {
      sessionStorage.setItem('studys', JSON.stringify({
        title: this.title,
        id: this.currentData.id,
      }))
      this.$router.push({
        path: '/studys',
      });
    },
    async toExam() {
      let examRule = '[]';
      if (this.examRule) {
        examRule = JSON.stringify(examRule);
      }
      this.showExam = true;
    },
    async endExam(){
      this.showExam = false
      const exam = await examInstance(this.currentData.id);
      const { state, message, examRule } = exam.data.data;
      this.state = state;
      this.examRule = examRule;
      if (state != 2) {
        this.$message.error(message);
        return;
      }
    }
  },
};
</script>

<style scoped lang="scss">
.wrap {
  height: calc(100vh - 110px);
  position: relative;
  z-index: 1;
}

.aside {
  transition: 0.5s all;
}

.h100 {
  height: 100%;
}

.right {
  flex: 1;
}

.hide {
  display: none;
}

.box {
  display: flex;
  height: 100%;
  overflow: auto;
  padding: 10px 10px 50px;
}

.close {
  font-size: 20px;
  float: right;
}

.flex-col {
  flex-direction: column;
}

:deep(.el-card__body) {
  padding: 10px !important;
}

.teamGroup {
  display: flex;
  flex-direction: column;

  .el-radio-button {
    width: 100%;
  }

  :deep(.el-radio-button__inner) {
    border-radius: 0 !important;
    border: 1px solid #eee;
    border-bottom: 0;
    width: 100%;
  }

  .el-radio-button:last-child :deep(.el-radio-button__inner) {
    border-bottom: 1px solid #eee;
    border-radius: 0 0 5px 5px !important;
  }

  .el-radio-button:first-child :deep(.el-radio-button__inner) {
    border-radius: 5px 5px 0 0 !important;
  }
}

:deep(.el-radio-button__original-radio:checked + .el-radio-button__inner) {
  font-weight: bolder;
}

:deep(.basic-container) {
  padding-bottom: 0 !important;
}

:deep(.basic-container__card) {
  height: 100%;
}

.position {
  position: relative;

  &::after {
    content: '';
    position: absolute;
    width: 13px;
    height: 26px;
    border-left: 1px #999 dashed;
    border-bottom: 1px #999 dashed;
    left: 2px;
    top: -10px;
  }

  &+.position::after {
    height: 46px;
    top: -30px;
  }

  &.direction::after {
    height: 46px;
    width: 34px;
    top: -30px;
  }

  &.direction::before {
    content: '';
    position: absolute;
    height: 28px;
    width: 13px;
    border-left: 1px #999 dashed;
    left: 24px;
    top: -13px;
  }

  &.direction+.direction::before {
    height: 44px;
    top: -27px;
  }
}

.positionSequence {
  position: relative;
  flex-shrink: 0;
  white-space: nowrap;
  z-index: 1;

  &::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 1px;
    border-bottom: 1px #999 dashed;
    top: 15px;
    left: 80px;
  }

  &:last-child::before {
    display: none;
  }
}

:deep(.el-card) {
  // background: #031527;
  color: #000;
}

.content :deep(.field) {
  position: relative;
  // box-shadow: 5px 5px 5px #999;
  padding-left: 20px;
}

.direction :deep(.field) {
  position: relative;
  padding-left: 40px;
}

:deep(.el-card__body) {
  height: 100%;
}

.content .el-button:hover {
  transform: scale(1.3);
  transition: all 0.075s cubic-bezier(0, 1.8, 1, 1.8);
}

:deep(.el-descriptions__body .el-descriptions__cell) {
  text-align: center !important;
}

.center :deep(.el-descriptions__body .el-descriptions__cell) {
  width: 50% !important;
}
</style>
<style>
#avue-view {
  margin-bottom: 0;
}
</style>
